#!/usr/bin/env bash

source $(dirname $0)/cmdTestsCommon.inc.sh

echo "testing scaladoc script"
clear_out "$OUT"
clear_out "$OUT1"


STDLIB_COMMAND="$SBT \"eval println(Build.stdlibVersion(Modes.NonBootstrapped))\""
STDLIB_VERSION=$(eval $STDLIB_COMMAND | tail -n 2 | head -n 1)

DOTTY_NONBOOTSTRAPPED_VERSION_COMMAND="$SBT \"eval println(Build.dottyNonBootstrappedVersion)\""
DOTTY_NONBOOTSTRAPPED_VERSION=$(eval $DOTTY_NONBOOTSTRAPPED_VERSION_COMMAND | tail -n 2 | head -n 1)

DOTTY_BOOTSTRAPPED_VERSION_COMMAND="$SBT \"eval println(Build.dottyVersion)\""
DOTTY_BOOTSTRAPPED_VERSION=$(eval $DOTTY_BOOTSTRAPPED_VERSION_COMMAND | tail -n 2 | head -n 1)

SOURCE_LINKS_REPOSITORY="scala/scala3"
SOURCE_LINKS_VERSION="${GITHUB_SHA:-$DOTTY_BOOTSTRAPPED_VERSION}"

"$SBT" "scaladoc/generateTestcasesDocumentation" > "$tmp" 2>&1 || echo "generated testcases project with sbt"
dist/target/universal/stage/bin/scaladoc \
  -d "$OUT1" \
  -project "scaladoc testcases" \
  -source-links:out/bootstrap/scala2-library-bootstrapped/scala-"${DOTTY_NONBOOTSTRAPPED_VERSION}"/src_managed/main/scala-library-src=github://scala/scala/v"${STDLIB_VERSION}"#src/library \
  -source-links:out/bootstrap/scala2-library-bootstrapped/scala-"${DOTTY_NONBOOTSTRAPPED_VERSION}"/src_managed/main/dotty-library-src=github://"${SOURCE_LINKS_REPOSITORY}"/"${SOURCE_LINKS_VERSION}"\#library/src \
  -source-links:github://"${SOURCE_LINKS_REPOSITORY}"/"${SOURCE_LINKS_VERSION}" \
  "-external-mappings:.*scala/.*::scaladoc3::https://dotty.epfl.ch/api/,.*java/.*::javadoc::https://docs.oracle.com/javase/8/docs/api/" \
  "-skip-by-regex:.+\.internal($|\..+)" \
  "-skip-by-regex:.+\.impl($|\..+)" \
  -project-logo docs/_assets/images/logo.svg \
  -social-links:github::https://github.com/scala/scala3,discord::https://discord.com/invite/scala,twitter::https://twitter.com/scala_lang \
  -Ygenerate-inkuire \
  "-skip-by-id:scala.runtime.stdLibPatches" \
  "-skip-by-id:scala.runtime.MatchCase" \
  "-snippet-compiler:scaladoc-testcases/docs=compile" \
  "-comment-syntax:scaladoc-testcases/src/example/comment-md=markdown,scaladoc-testcases/src/example/comment-wiki=wiki" \
  -siteroot scaladoc-testcases/docs \
  -project-footer "Copyright (c) 2002-$(date +%Y), LAMP/EPFL" \
  -default-template static-site-main \
  -author -groups -revision main -project-version "${DOTTY_BOOTSTRAPPED_VERSION}" \
  "-quick-links:Learn::https://docs.scala-lang.org/,Install::https://www.scala-lang.org/download/,Playground::https://scastie.scala-lang.org,Find A Library::https://index.scala-lang.org,Community::https://www.scala-lang.org/community/,Blog::https://www.scala-lang.org/blog/," \
  out/bootstrap/scaladoc-testcases/scala-"${DOTTY_NONBOOTSTRAPPED_VERSION}"/classes > "$tmp" 2>&1 || echo "generated testcases project with scripts"
diff -r "$OUT1" "scaladoc/output/testcases"
